dsu greedy implementation *1200

Please click on ads to support us..

Python Code:

import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os
def valid(i,j,n,m):
        if i<n and i>=0 and j>=0 and j< m :return True         return  False
def sumn(i,n):
    return (n-i)*(i+n)/2
def sqfun(a,b,c):
    return (-b+math.sqrt(b*b-4*a*c))/2*a



def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return 
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]
def words(): return [i for i in input().split()]


def f():
    dd={}
    i=0
    for w in sys.stdin.readline().split():
            dd[w]=i
            i+=1
            wd.append(w)
    return dd
def kin():
    
    l=[int(i)-1 for i in input().split()]
    nn=l.pop(0)
    l=sorted(l)
    mn=cost[l[0]]
    for i in l:
        mn=min(mn,cost[i])
        d[wd[i]]=l[0]
    cost[l[0]]=mn
def solve():
    global wd ,d,cost
    wd=[]
    n,k,m=values()
    d=f()
    cost=inlsts()
    for i in range(k):
        kin()
    tot=0
    r=words()
    for i in r:tot+=cost[d[i]]
    return tot





if __name__ == "__main__":
           print(solve()) 

C++ Code:

#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
#define en cout<<endl;
using namespace std;
void fast(){
    std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
//int fre[10000004]={0};

void solve(){
	int n,k,m1;
	cin>>n>>k>>m1;
	pair<string,int> arr[n];
	map<string,int>mp;
	for(int i=0;i<n;i++){
		cin>>arr[i].first;
	}
	for(int i=0;i<n;i++){
		cin>>arr[i].second;
		mp[arr[i].first]=arr[i].second;
	}
	for(int i=0;i<k;i++){
		int m;
		cin>>m;
		int ar[m],mn;
		for(int j=0;j<m;j++){
			cin>>ar[j];
			if(!j)mn=arr[ar[j]-1].second;
			else mn=min(mn,arr[ar[j]-1].second);
		}
		for(int j=0;j<m;j++){
			arr[ar[j]-1].second=min(mn,arr[ar[j]-1].second);
			mp[arr[ar[j]-1].first]=arr[ar[j]-1].second;
		}
	}
	ll ans=0;
	for(int i=0;i<m1;i++){
		string s;
		cin>>s;
		ans+=mp[s];
	}
	cout<<ans;
}

int main(){
    fast();
    int t=1;
//    cin>>t;
    while(t--){
        solve();
        en
    }
    
}
/*

4 2
4
1 2 3 4
4
1 2 3 4


*/


Comments

Submit
0 Comments
More Questions

1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha